﻿using System.Web.Security;
using Comdiv.QWeb.Utils;

namespace Comdiv.QWeb.SystemActions {
	[Action("qweb.login", Help = "EN: used to authenticate with form based method")]
	public class LoginAction : ActionBase {
		[Bind(Name = "_l_o_g_i_n_", Required = true, ValidatePattern = @"^[\w\.-]+$")] private string login;
		[Bind(Name = "_p_a_s_s_", Required = true, ValidatePattern = @"^[\w\S]{8,}$")] private string pass;

		protected override object process() {
			if (login.hasContent()) {
				if (FormsAuthentication.Authenticate(login, pass) ||
				    Registry.FormAuthenticator.IsAuthenticated(login, pass, Context)) {
					FormsAuthentication.RedirectFromLoginPage(login, false);
					return new {needform = false, login};
				}
			}
			return new {needform = true, login};
		}
	}
}